package com.lbw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lbw.config.BasePage;
import com.lbw.entity.SubjectEntity;
import com.lbw.entity.VideoEntity;
import com.lbw.mapper.SubjectEntityMapper;
import com.lbw.mapper.VideoEntityMapper;
import com.lbw.service.VideoEntityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lbw
 * @since 2020-09-13
 */
@Service
public class VideoEntityServiceImpl extends ServiceImpl<VideoEntityMapper, VideoEntity> implements VideoEntityService {

    @Resource
    private VideoEntityMapper videoEntityMapper;

    @Resource
    private SubjectEntityMapper subjectEntityMapper;

    @Override
    public List<VideoEntity> selectVideoBySubjectId(String subjectId, String stuId) {

        return videoEntityMapper.selectVideoBySubjectId(subjectId,stuId);
    }

    @Override
    public boolean updateVideoCompleteTime(String id, double currentTime) {

        //通过id获取视频信息
        VideoEntity videoEntity = videoEntityMapper.selectById(id);
        //计算分钟数
        DecimalFormat df = new DecimalFormat("#.00");
        Double ls = Double.valueOf(df.format(currentTime / 60));
        videoEntity.setCompleteLength(ls);
        //设置视频完成时间
        int update = videoEntityMapper.updateById(videoEntity);
        if (update == 1) {
            //获取科目
            LambdaQueryWrapper<SubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SubjectEntity::getType,videoEntity.getSubject()).eq(SubjectEntity::getStuId,videoEntity.getStuId());
            //SubjectEntity subjectEntity = subjectEntityMapper.selectById(videoEntity.getSubject());
            SubjectEntity subjectEntity = subjectEntityMapper.selectOne(queryWrapper);
            //通过科目id查询出各个章节的视频完成量
            LambdaQueryWrapper<VideoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(VideoEntity::getSubject,subjectEntity.getType()).eq(VideoEntity::getStuId,subjectEntity.getStuId());
            List<VideoEntity> videoEntityList = videoEntityMapper.selectList(lambdaQueryWrapper);
            //设置科目的完成时间
            double completeLength = 0;
            for (VideoEntity video : videoEntityList) {
                completeLength += video.getCompleteLength();
            }
            subjectEntity.setCompleteLength(completeLength);
            //获取总时间
            Double allLength = subjectEntity.getAllLength();
            if (allLength > completeLength && allLength - completeLength < 1) {
                //小于一分钟，则认为完成，完成率为100%
                subjectEntity.setProportion((double) 1);
            }else {
                subjectEntity.setProportion(Double.valueOf(df.format(completeLength / allLength)));
            }

            int updateSubject = subjectEntityMapper.updateById(subjectEntity);
            return updateSubject == 1;
        }

        return false;
    }

    @Override
    public List<Map<String,Object>> selectVideoBySubjectAndChapter(String subjectId, String chapterId, String stuId) {
        List<Map<String,Object>> subjectAndChapter = videoEntityMapper.selectVideoBySubjectAndChapter(subjectId, chapterId,stuId);

        /*LambdaQueryWrapper<VideoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(VideoEntity::getSubject,subjectId).eq(VideoEntity::getChapter,chapterId);*/
        return subjectAndChapter;
    }
}
